﻿using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using WqLunTan.Models;
using WqLunTan.Models.DataBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WqLunTan.DBContext
{
    /// <summary>
    /// Mysql数据库连接上下文
    /// </summary>
    public class MySqlDbContext : IdentityDbContext<WqUser, WqUserRole, Guid>
    {
        private static string connectionString = @"Database=wqluntan;Data Source=127.0.0.1;User Id=wqluntan;Password=wqluntan;CharSet=utf8;port=3306";
        private static DbContextOptions<MySqlDbContext> dbContextOption = new DbContextOptions<MySqlDbContext>();
        private static DbContextOptionsBuilder<MySqlDbContext> dbContextOptionBuilder = new DbContextOptionsBuilder<MySqlDbContext>(dbContextOption);

        /// <summary>
        /// new 一个DbContext,一定要手动释放对象
        /// </summary>
        /// <param name="connection"></param>
        /// <returns></returns>
        public static MySqlDbContext GetDbContext(string connection)
        {
            if (string.IsNullOrWhiteSpace(connection))
            {
                connection = connectionString;
            }
            return new MySqlDbContext(dbContextOptionBuilder.UseMySql(connection).Options);
        }

        /// <summary>
        /// 如果数据库不存在, 自动创建数据库
        /// </summary>
        /// <param name="options"></param>
        public MySqlDbContext(DbContextOptions<MySqlDbContext> options) : base(options)
        {
            Database.EnsureCreated();
        }

        /// <summary>
        /// 模型映射绑定
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            var userEntry = modelBuilder.Entity<WqUser>();
            userEntry.HasIndex(x => x.OpenIdQQ);
            userEntry.HasIndex(x => x.OpenIdWechat);
            userEntry.HasIndex(x => x.OpenIdBaidu);
            userEntry.HasIndex(x => x.OpenIdAli);
            userEntry.HasIndex(x => x.NickName).IsUnique();
            userEntry.HasIndex(x => x.UserName).IsUnique();

            var cateEntry = modelBuilder.Entity<article_category>();
            cateEntry.HasIndex(x => x.category_name).IsUnique();

            var siteConfigEntry = modelBuilder.Entity<site_config>();
            siteConfigEntry.HasIndex(x => x.config_key).IsUnique();

            var articleCrawl = modelBuilder.Entity<article_crawler>();
            articleCrawl.HasIndex(x => x.unique_hash).IsUnique();
        }

        /// <summary>
        /// 微信接口推送数据
        /// </summary>
        public DbSet<wechat_data> WeChatData { get; set; }

        /// <summary>
        /// 微信公众号的用户
        /// </summary>
        public DbSet<wechat_openid> WeChatOpenId { get; set; }

        /// <summary>
        /// 网站后台回复的微信消息
        /// </summary>
        public DbSet<wechat_reply> WeChatReply { get; set; }

        /// <summary>
        /// 网站头部的广告位
        /// </summary>
        public DbSet<site_layout_content> SiteHeader { get; set; }

        public DbSet<article> article { get; set; }

        /// <summary>
        /// 热门文章
        /// </summary>
        public DbSet<article_hot> article_hots { get; set; }

        /// <summary>
        /// 预测历史
        /// </summary>
        public DbSet<article_forecast> art_forecast { get; set; }

        /// <summary>
        /// 文章分类表
        /// </summary>
        public DbSet<article_category> art_category { get; set; }


        public DbSet<liuyanban> liuyanban { get; set; }

        public DbSet<login_log> login_log { get; set; }

        public DbSet<notice> notice { get; set; }
        public DbSet<post_content> PostContent { get; set; }

        public DbSet<post_inform> PostInform { get; set; }

        public DbSet<post_title> PostTitle { get; set; }

        public DbSet<post_reply> PostReply { get; set; }

        /// <summary>
        /// 站点配置
        /// </summary>
        public DbSet<site_config> SiteConfig { get; set; }

        /// <summary>
        /// 爬虫获取到的文章
        /// </summary>
        public DbSet<article_crawler> ArticleCrawls { get; set; }

    }
}
